package org.gruposp2p.dnie.crypto;

import java.io.FileInputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertPath;
import java.security.cert.CertPathValidator;
import java.security.cert.CertPathValidatorException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.PKIXCertPathValidatorResult;
import java.security.cert.PKIXParameters;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author jj
 */
public class OCSPValidator {

/*
    X509Certificate certificate = certificateChain[0];
PublicKey publicKey = certificate.getPublicKey();

CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
CertPath certificatePath = certificateFactory.generateCertPath(certificateChainList);

PKIXParameters pkixParameters = new PKIXParameters(trustedKeyStore);

CertPathValidator certificatePathValidator = CertPathValidator.getInstance("PKIX");
certificatePathValidator.validate(certificatePath, pkixParameters);
*/

    public PKIXCertPathValidatorResult validate (FileInputStream in) 
            throws CertificateException, InvalidAlgorithmParameterException,
            NoSuchAlgorithmException, CertPathValidatorException {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        List mylist = new ArrayList();
        Certificate c = cf.generateCertificate(in);
        mylist.add(c);
        CertPath cp = cf.generateCertPath(mylist);
        Certificate trust = cf.generateCertificate(in);
        TrustAnchor anchor = new TrustAnchor((X509Certificate) trust, null);
        PKIXParameters params = new PKIXParameters(Collections.singleton(anchor));
        params.setRevocationEnabled(false);
        CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
        PKIXCertPathValidatorResult result = (
                PKIXCertPathValidatorResult) cpv.validate(cp, params);
        return result;
    }

}
